<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\api\model\Collect;
use app\api\model\Complaint;
use app\api\model\House as HouseM;
use app\api\model\Order;
use app\api\model\Refund;
use app\api\model\User AS UserM;
use app\api\model\UserMoneyLog;
use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use Yansongda\Pay\Pay;
/**
 * 我的接口
 */
class User extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];


    /**
     * 个人信息
     * @return void
     */
    public function info(){
        $user=$this->auth;
        $data['id']=$user->id;
        $data['nickname']=$user->nickname;
        $data['avatar']=$user->avatar;
        $data['mobile']=$user->mobile;
        $data['money']=$user->money;

        $data['qr_code']=$this->qrcode($user->id);
        $data['improve']=$user->improve;
        $data['is_business']=$user->is_business;
        $is_sign_in=0;

        $where_sign['user_id']=$user->id;
        $where_sign['time']=['>',strtotime(date('Y-m-d 0:0:1',time()))];
        $user_sign_find=db('user_sign')->where($where_sign)->find();
        if(!empty($user_sign_find)){
           $is_sign_in=1;
        }
        $data['is_sign_in']=$is_sign_in;
        $data['share_num']=$user->share_num;

        $UserMoneyLog = new UserMoneyLog();
        $where_log['user_id']=$user->id;
        $where_log['memo']="分享获得钢豆";
        $where_log['createtime']=['>',strtotime(date('Y-m-d 0:0:1',time()))];
        $this_day_share=$UserMoneyLog->where($where_log)->order('id desc')->sum('money');
        $data['this_day_share']=$this_day_share;
        $data['is_unread']=$user->is_unread;
        $this->success('',$data);
    }
    public function minus_share_num()
    {
        $user=$this->auth;
        $UserM = new UserM();
        if($user->share_num<=0){
            $this->error('错误');
        }
        $where['id']=$user->id;
        $UserM->where($where)->setDec('share_num');
        $this->success('');
    }
    public function user_sign()
    {
        $user=$this->auth;
        $where_sign['user_id']=$user->id;
        $where_sign['time']=['>',strtotime(date('Y-m-d',time()))];
        $user_sign_find=db('user_sign')->where($where_sign)->find();
        if(!empty($user_sign_find)){
           $this->error('您已签到,请勿重复签到');
        }else{
            $data['user_id']=$user->id;
            $data['time']=time();
            db('user_sign')->insert($data);

            $sign_in=$this->config_data('sign_in');
            $this->user_money($user->id,$sign_in,1,"签到获得钢豆");
        }
        $this->success('');
    }
    /**
     * 用户分享二维码
     * @param mixed $ids
     * @return string
     */
    public function qrcode($ids = "")
    {
        header('content-type:image/jpeg');
        $access_token = $this->access_token();

        $data['path'] = "pages/index/index?pid=" . $ids;

        $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" . $access_token['access_token'];
        $return = $this->wx_curl(json_encode($data), $url);
        $file = "/uploads/smallwechat/" . $ids . ".jpg";
        file_put_contents("." . $file, $return);
        return $file;
    }

    public function access_token(){

        $appid = $this->config_data('appid');//小程序的appid
        $secret = $this->config_data('secret');// 小程序的$appSecret

        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;

        $result=$this->curl_get($url);
        $wxResult = json_decode($result, true);
        return $wxResult;
    }
    public function wx_curl($vars,$url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //没有这个会自动输出，不用print_r();也会在后面多个1
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$vars);
        $output = curl_exec($ch);
        curl_close($ch);
        $out = json_decode($output);
        return $output;
    }
    public function curl_get($url, &$httpCode = 0) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //不做证书校验,部署在linux环境下请改为true
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $file_contents = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $file_contents;
    }


    /**
     * 修改个人信息
     * @return void
     */
    public function edit_info(){
        $param = request()->param();
        $UserM = new UserM();
        $user=$this->auth;
        $where['id']=$user->id;
        $updata=$param;
        $UserM->save($updata,$where);
        $this->success('修改成功');
    }

    /**
     *钢豆记录
     * @return void
     */
    public function MoneyLog()
    {
        $user=$this->auth;
        $UserMoneyLog = new UserMoneyLog();
        $where['user_id']=$user->id;
        $list=$UserMoneyLog->where($where)->order('id desc')->paginate();
        $this->success('',$list);
    }

    /**
     * 修改手机号
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit_mobile(){
        $param = request()->param();
        $UserM = new UserM();
        $user=$this->auth;
        $ret = Smslib::check($param['mobile'], $param['code']);
        if(!$ret){
            $this->error('验证码错误');
        }
        $where['mobile']=$param['mobile'];
        $find=$UserM->where($where)->find();
        if(!empty($find)){
            $this->error('此手机号已使用');
        }
        $updata['mobile']=$param['mobile'];
        $where_up['id']=$user->id;
        $UserM->save($updata,$where_up);
        $this->success('修改成功');
    }

    /**
     * 投诉建议
     * @return void
     */
    public function complaint(){
        $param = request()->param();
        $user=$this->auth;
        $Complaint = new Complaint();
        $Complaint->user_id=$user->id;
        $Complaint->propose=$param['propose'];
        $Complaint->group_images=$param['group_images'];
        $Complaint->name=$param['name'];
        $Complaint->phone=$param['phone'];
        $Complaint->save();
        $this->success('请求成功');
    }

    /**
     * 收藏列表
     * @return void
     * @throws \think\exception\DbException
     */
    public function collect(){
        $param = request()->param();
        $user=$this->auth;
        $Collect = new Collect();
        $HouseM = new HouseM();
        $where['user_id']=$user->id;
        $where['type']=$param['type'];
        $list=$Collect->where($where)->paginate()->each(function ($item)use($HouseM){
            $find_house=$HouseM->get($item['house_id']);
            if(empty($find_house)){
                $item['is_lose']=0;
            }else{
                $item['is_lose']=1;
                $item['house']=$find_house;
            }
            return $item;
        });
        $this->success('',$list);
    }

    /**
     * 我的发布
     * @return void
     * @throws \think\exception\DbException
     */
    public function house(){
        $param = request()->param();
        $user=$this->auth;
        $HouseM = new HouseM();
//        $Order = new Order();
        if(!empty($param['introduce'])){
            $where['introduce']=['like',"%".$param['introduce']."%"];
        }
        if($param['type']==1){
            $where['is_arrive']=1;
            $where['is_careful']=1;
            $where['status']=['in','1,2'];
        }
        if($param['type']==2){
            $where['is_careful']=0;
            $where['status']=['in','1,2'];
        }
        if($param['type']==3){
            $where['is_careful']=['in','1,2'];
//            $where['is_careful']=1;
            $where['is_arrive']=0;
            $where['status']=['in','1,2'];
        }
        if($param['type']==4){
            $where['status']=['in','3'];
//            $where['is_careful']=2;
        }

        $where['user_id']=$user->id;
        $where['is_pay']=1;
        $where['is_del']=0;
        $field="*";
        $list=$HouseM->where($where)->field($field)->order('id desc')->paginate();
        $this->success('',$list);
    }
    public function InformationCount()
    {
        $user=$this->auth;
        $HouseM = new HouseM();
        $where_type1['is_arrive']=1;
        $where_type1['is_careful']=1;
        $where_type1['user_id']=$user->id;
        $where_type1['is_pay']=1;
        $where_type1['status']=['in',1,2];
        $type1=$HouseM->where($where_type1)->count();

        $where_type2['is_careful']=0;
        $where_type2['user_id']=$user->id;
        $where_type2['is_pay']=1;
        $where_type2['status']=['in',1,2];
        $type2=$HouseM->where($where_type2)->count();

        $where_type3['is_careful']=2;
        $where_type3['user_id']=$user->id;
        $where_type3['is_pay']=1;
        $where_type2['status']=['in',1,2];
        $type3=$HouseM->where($where_type3)->count();


        $where_type4['user_id']=$user->id;
        $where_type4['is_pay']=1;
        $where_type4['status']=['in',3];
        $type4=$HouseM->where($where_type4)->count();

        $data['type1']=$type1;
        $data['type2']=$type2;
        $data['type3']=$type3;
        $data['type4']=$type4;
        $this->success('',$data);
    }

    /**
     * 上/下架房源
     * @return void
     */
    public function shelf_house(){
        $param = request()->param();
        $user=$this->auth;
        $HouseM = new HouseM();
        $where['user_id']=$user->id;
        $where['id']=$param['id'];
        $updara['is_arrive']=$param['is_arrive'];
        $HouseM->save($updara,$where);
        $this->success('成功');
    }

}
